package com.logic.modular.game.player.info.service;


import com.comment.pojo.domain.logic.domain.PlayerStatus;
import com.logic.modular.game.player.info.mapper.PlayerStatusMapper;
import com.logic.modular.game.player.info.PlayerStatusBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 玩家对局信息(PlayerStatus)表服务实现类
 *
 * @author cxy
 * @since 2024-08-19 16:41:20
 */
@Slf4j
@Service
public class PlayerStatusServiceImpl extends PlayerStatusBaseServiceImpl {
    @Resource
    private PlayerStatusMapper playerMatchMapper;

    public Long currentMatchId(Long playerId) {
        PlayerStatus playerMatch = playerMatchMapper.selectById(playerId);
        if (playerMatch == null) {
            return 0L;
        }
        return playerMatch.getCurrentMatchId();
    }

    public PlayerStatus obtainPlayerStatus(Long playerId) {
        PlayerStatus byId = getById(playerId);
        if (byId == null) {
            byId = new PlayerStatus();
            byId.setUid(playerId);
            byId.setCurrentMatchId(0L);
            byId.setRoomOwnerId(0L);
            byId.insert();
        }
        return byId;
    }

    /**
     * 更新玩家当前对局id  -1 代表处于匹配池
     */
    public void updateMatch(
            @NotNull(message = "playerId不能为空") Long playerId,
            @NotNull(message = "pvpMatchId不能为空") Long pvpMatchId
    ) {
        PlayerStatus playerStatus = obtainPlayerStatus(playerId);
        playerStatus.setCurrentMatchId(pvpMatchId);
        updateById(playerStatus);
    }

    /**
     * 重置玩家 pvp 匹配信息
     */
    public void resetPvp(Long playerId) {
        PlayerStatus playerMatch = get(playerId);
        playerMatch.setCurrentMatchId(0L);
        updateById(playerMatch);
    }


}
